package com.example.matchandroiddevices.scanlable;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.matchandroiddevices.R;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;

public class RW_Activity extends Activity implements OnClickListener, OnItemSelectedListener{
	
	Spinner spuid;
	Spinner spant;
	Spinner sptype;
	private ArrayAdapter<String> spada_uid; 
	private ArrayAdapter<String> spada_ant; 
	private ArrayAdapter<String> spada_type; 
	TextView tvResult;
	EditText tvstartblock; 
	EditText tvblocknum; 
	EditText tvwdata; 
	EditText tvrdata; 
	EditText txtAfi; 
	EditText txtDsfid; 
	Button btReadSingle;
	Button btReadMul;
	Button btWriteSingle;
	Button btSetAntenna;
	Button btSetAFI;
	Button btGetAFI;
	Button btLockAFI;
	Button btSetDSFID;
	Button btGetDSFID;
	Button btLockDSFID;
	Button btEnableEAS;
	Button btDisableEAS;
	Button btCheckEAS;
	private static final int MSG_SHOW_RESULT = 0;
	private static final int MSG_SHOW_DATA = 1;
	private static final int MSG_SHOW_AFI = 2;
	private static final int MSG_SHOW_DSFID = 3;
	private Handler mHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rw_data);
		initView();
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case MSG_SHOW_RESULT:
					writelog((String)msg.obj, tvResult);
					break;
				case MSG_SHOW_DATA:
					tvrdata.setText((String)msg.obj);
					break;
				case MSG_SHOW_AFI:
					txtAfi.setText((String)msg.obj);
					break;
				case MSG_SHOW_DSFID:
					txtDsfid.setText((String)msg.obj);
					break;
				default:
					break;
				}
			}
		};  
	}
	
	
	private void UpdateErrorToast(String errormsg)
	{
		if((errormsg==null)||(errormsg.length()==0))return;
		Message message = mHandler.obtainMessage();
		message.what = MSG_SHOW_RESULT;
		message.obj = errormsg;
		mHandler.sendMessage(message);
	}
	
	
	private void UpdateBLKData(String datastr)
	{
		if((datastr==null)||(datastr.length()==0))return;
		Message message = mHandler.obtainMessage();
		message.what = MSG_SHOW_DATA;
		message.obj = datastr;
		mHandler.sendMessage(message);
	}
	
	private void UpdateAFI(String datastr)
	{
		if((datastr==null)||(datastr.length()==0))return;
		Message message = mHandler.obtainMessage();
		message.what = MSG_SHOW_AFI;
		message.obj = datastr;
		mHandler.sendMessage(message);
	}
	
	private void UpdateDSFID(String datastr)
	{
		if((datastr==null)||(datastr.length()==0))return;
		Message message = mHandler.obtainMessage();
		message.what = MSG_SHOW_DSFID;
		message.obj = datastr;
		mHandler.sendMessage(message);
	}
	private void initView(){
		spuid = (Spinner)findViewById(R.id.uid_spinner);  
		tvstartblock = (EditText)findViewById(R.id.et_wordptr);	
		tvblocknum = (EditText)findViewById(R.id.et_num);	
		tvwdata = (EditText)findViewById(R.id.et_content);	
		tvrdata = (EditText)findViewById(R.id.et_read);	
		tvResult = (TextView)findViewById(R.id.rw_result);	
		txtAfi = (EditText)findViewById(R.id.txtAfi);
		txtDsfid = (EditText)findViewById(R.id.txtDsfid);
		
		btReadSingle = (Button)findViewById(R.id.button_read);	
		btReadMul = (Button)findViewById(R.id.button_readmul);	
		btWriteSingle = (Button)findViewById(R.id.button_write);
		btSetAntenna = (Button)findViewById(R.id.button_setant);
		btSetAFI = (Button)findViewById(R.id.btSetAfi);
		btGetAFI = (Button)findViewById(R.id.btGetAfi);
		btLockAFI = (Button)findViewById(R.id.btLockAfi);
		btLockDSFID = (Button)findViewById(R.id.btLockDsfid);
		btSetDSFID = (Button)findViewById(R.id.btSetDsfid);
		btGetDSFID = (Button)findViewById(R.id.btGetDsfid);
		btEnableEAS = (Button)findViewById(R.id.btEnableEAS);
		btDisableEAS = (Button)findViewById(R.id.btDisableEAS);
		btCheckEAS = (Button)findViewById(R.id.btCheckEAS);
		
		btReadSingle.setOnClickListener(this);
		btReadMul.setOnClickListener(this);
		btWriteSingle.setOnClickListener(this);
		btSetAntenna.setOnClickListener(this);
		btSetAFI.setOnClickListener(this);
		btGetAFI.setOnClickListener(this);
		btLockAFI.setOnClickListener(this);
		
		btLockDSFID.setOnClickListener(this);
		btSetDSFID.setOnClickListener(this);
		btGetDSFID.setOnClickListener(this);
		
		btEnableEAS.setOnClickListener(this);
		btDisableEAS.setOnClickListener(this);
		btCheckEAS.setOnClickListener(this);
		
		String[]AntArr = new String[4];
		for(int m=0;m<AntArr.length;m++)
		{
			AntArr[m] ="天线"+String.valueOf(m+1);
		}
		spant = (Spinner)findViewById(R.id.culant_spinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,AntArr);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spant.setAdapter(adapter); 
		spant.setSelection(0, true);
		
		sptype = (Spinner)findViewById(R.id.mtype_spinner);
		ArrayAdapter<CharSequence> adapter1 =  ArrayAdapter.createFromResource(this, R.array.type_spinner, android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sptype.setAdapter(adapter1); 
		sptype.setSelection(1, true);
		sptype.setOnItemSelectedListener(this);
	   	

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if(ScanMode.mCurIvtClist!=null)
		{
			int nCount = ScanMode.mCurIvtClist.size();
			if(nCount>0)
			{
				String[]uiddata = new String[nCount];
				for (int i = 0; i < nCount; i++) {
			            HashMap<String, String> temp = new HashMap<String, String>();
			            temp = ScanMode.mCurIvtClist.get(i);
			            String uidStr = temp.get("tagUid");
			            String antStr = temp.get("tagAnt");
			            uiddata[i] = uidStr+"-"+antStr;
			        }
				if(nCount>0)
				{
					spada_uid = new ArrayAdapter<String>(RW_Activity.this,  
			                android.R.layout.simple_spinner_item, uiddata);  
					spada_uid.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
				   	spuid.setAdapter(spada_uid);  
				   	spuid.setSelection(0,false);
				}
			}
			else
			{
				spuid.setAdapter(null); 
			}
		}
		else
		{
			spuid.setAdapter(null); 
		}
		
		super.onResume();
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	byte Antenna=0;
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if(arg0 == btReadSingle){
			if(!checkContent(0)) return;
			try{
				
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte state=0;
				final byte[]UID =  hexStringToBytes(str);
				final byte blockNumber=(byte)(int)Integer.valueOf(tvstartblock.getText().toString());
				final byte[]SecurityWord=new byte[1];
				final byte[]Data=new byte[4];
				final byte[]ErrorCode =new byte[1];
				tvrdata.setText("");
				
				new Thread()
		        {
		            public void run() 
		            {
		            	int result =HfData.reader.ReadSingleBlock(state, UID, blockNumber, SecurityWord, Data, ErrorCode);
						if(result==0)
						{
							String strdata = bytesToHexString(Data,0,Data.length);
							UpdateBLKData(strdata);
							UpdateErrorToast("读取成功");
						}
						else
						{
							UpdateBLKData("");
							UpdateErrorToast("读取失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateBLKData("");
				UpdateErrorToast("读取失败");
			}
		}
		else if(arg0 == btReadMul)
		{
			if(!checkContent(1)) return;
			try{
				
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte state=0;
				final byte[]UID = hexStringToBytes(str);
				final byte startblockNumber=(byte)(int)Integer.valueOf(tvstartblock.getText().toString());
				final byte numberOfBlock=(byte)(int)Integer.valueOf(tvblocknum.getText().toString());
				final byte[]SecurityWord=new byte[numberOfBlock];
				final byte[]Data=new byte[numberOfBlock*4];
				final byte[]ErrorCode =new byte[1];
				tvrdata.setText("");
				
				new Thread()
		        {
		            public void run() 
		            {
		            	int result =0;
		            //	while(true)
		            	{
		            		result =HfData.reader.ReadMultipleBlock(state, UID, startblockNumber, numberOfBlock, SecurityWord, Data, ErrorCode);
							if(result==0)
							{
								String strdata = bytesToHexString(Data,0,Data.length);
								UpdateBLKData(strdata);
								UpdateErrorToast("读取成功");
							}
							else
							{
								UpdateBLKData("");
								UpdateErrorToast("读取失败");
							}
		            	}
		            };
		        }.start();
				
			}catch(Exception ex)
			{
				UpdateBLKData("");
				UpdateErrorToast("读取失败");
			}
		}
		else if(arg0 == btWriteSingle)
		{
			if(!checkContent(2)) return;
			try{
				String strData="";
				
				strData = tvwdata.getText().toString();
				if((strData==null)||(strData.length()!=8))
				{
					writelog("写入数据长度不符", tvResult);
					return;
				}
				final byte mType = (byte)sptype.getSelectedItemPosition();
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte blockNumber=(byte)(int)Integer.valueOf(tvstartblock.getText().toString());
				final byte[]Data= hexStringToBytes(strData);
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
						if(mType==0)
							state=0;
						else
							state=8;
		            	int result =HfData.reader.WriteSingleBlock(state, UID, blockNumber, Data, ErrorCode);
						if(result==0)
						{
							UpdateErrorToast("写入成功");
						}
						else
						{
							UpdateErrorToast("写入失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("写入失败");
			}
		}
		else if(arg0 == btSetAntenna)
		{
			try{
				byte mAntenna = (byte)spant.getSelectedItemPosition();
				switch(mAntenna)
				{
				case 0:
					Antenna=1;
					break;
				case 1:
					Antenna=2;
					break;
				case 2:
					Antenna=4;
					break;
				case 3:
					Antenna=8;
					break;
				}
				new Thread()
		        {
		            public void run() 
		            {
		            	int result =HfData.reader.SetAntenna(Antenna);
						if(result==0)
						{
							UpdateErrorToast("设置成功");
						}
						else
						{
							UpdateErrorToast("设置失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("设置失败");
			}
		}
		else if(arg0 == btSetAFI)
		{
			if(!checkContent(3)) return;
			try{
				String strData="";
				
				strData = txtAfi.getText().toString();
				if((strData==null)||(strData.length()>4))
				{
					writelog("写入AFI数据不符", tvResult);
					return;
				}
				final byte mType = (byte)sptype.getSelectedItemPosition();
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte afi = (byte)((int)Integer.valueOf(strData));
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
						if(mType==0)
							state=0;
						else
							state=8;
		            	int result =HfData.reader.WriteAFI(state, UID, afi, ErrorCode);
						if(result==0)
						{
							UpdateErrorToast("写入AFI成功");
						}
						else
						{
							UpdateErrorToast("写入AFI失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("写入AFI失败");
			}
		}
		else if(arg0 == btLockAFI)
		{
			try{
				
				final byte mType = (byte)sptype.getSelectedItemPosition();
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
						if(mType==0)
							state=0;
						else
							state=8;
		            	int result =HfData.reader.LockAFI(state, UID, ErrorCode);
						if(result==0)
						{
							UpdateErrorToast("锁定AFI成功");
						}
						else
						{
							UpdateErrorToast("锁定AFI失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("锁定AFI失败");
			}
		}
		else if(arg0 == btGetAFI)
		{
			try{
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
		            	byte[]InformationFlag=new byte[1];
		            	byte[]DSFID=new byte[1];
		            	byte[]AFI=new byte[1];
		            	byte[]MemorySize=new byte[2];
		            	int result =HfData.reader.GetSystemInformation(state, UID, InformationFlag, 
		            			DSFID, AFI, MemorySize);
						if(result==0)
						{
							UpdateAFI(String.valueOf(AFI[0]&255));
							UpdateErrorToast("读取AFI成功");
						}
						else
						{
							UpdateAFI("");
							UpdateErrorToast("读取AFI失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateAFI("");
				UpdateErrorToast("读取AFI失败");
			}
		}///
		else if(arg0 == btSetDSFID)
		{
			if(!checkContent(4)) return;
			try{
				String strData="";
				
				strData = txtDsfid.getText().toString();
				if((strData==null)||(strData.length()>4))
				{
					writelog("写入DSFID数据不符", tvResult);
					return;
				}
				final byte mType = (byte)sptype.getSelectedItemPosition();
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte dsfid = (byte)((int)Integer.valueOf(strData));
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
						if(mType==0)
							state=0;
						else
							state=8;
		            	int result =HfData.reader.WriteDSFID(state, UID, dsfid, ErrorCode);
						if(result==0)
						{
							UpdateErrorToast("写入DSFID成功");
						}
						else
						{
							UpdateErrorToast("写入DSFID失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("写入DSFID失败");
			}
		}
		else if(arg0 == btLockDSFID)
		{
			try{
				final byte mType = (byte)sptype.getSelectedItemPosition();
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
						if(mType==0)
							state=0;
						else
							state=8;
		            	int result =HfData.reader.LockDSFID(state, UID, ErrorCode);
						if(result==0)
						{
							UpdateErrorToast("锁定DSFID成功");
						}
						else
						{
							UpdateErrorToast("锁定DSFID失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("锁定DSFID失败");
			}
		}
		else if(arg0 == btGetDSFID)
		{
			try{
				
				if(spuid.getSelectedItem()==null)
				{
					writelog("请先选择一张标签操作", tvResult);
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				final byte[]ErrorCode=new byte[1];
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=0;
		            	byte[]InformationFlag=new byte[1];
		            	byte[]DSFID=new byte[1];
		            	byte[]AFI=new byte[1];
		            	byte[]MemorySize=new byte[2];
		            	int result =HfData.reader.GetSystemInformation(state, UID, InformationFlag, 
		            			DSFID, AFI, MemorySize);
						if(result==0)
						{
							UpdateDSFID(String.valueOf(DSFID[0]&255));
							UpdateErrorToast("读取DSFID成功");
						}
						else
						{
							UpdateDSFID("");
							UpdateErrorToast("读取DSFID失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateDSFID("");
				UpdateErrorToast("读取DSFID失败");
			}
		}
		else if(arg0 == btEnableEAS)
		{
			try{
				if(spuid.getSelectedItem()==null)
				{
					new Thread()
			        {
			            public void run() 
			            {
			            	byte state=0;
			            	int ReadType =GetReaderType();
			            	byte[]UID=new byte[8];
			            	int result =HfData.reader.EnableEAS(state, UID, ReadType);
							if(result==0)
							{
								UpdateErrorToast("Enable EAS成功");
							}
							else
							{
								UpdateErrorToast("Enable EAS失败");
							}
			            };
			        }.start();
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=1;
		            	int ReadType =GetReaderType();
		            	int result =HfData.reader.EnableEAS(state, UID, ReadType);
						if(result==0)
						{
							UpdateErrorToast("Enable EAS成功");
						}
						else
						{
							UpdateErrorToast("Enable EAS失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("Enable EAS失败");
			}
		}
		else if(arg0 == btDisableEAS)
		{
			try{
				if(spuid.getSelectedItem()==null)
				{
					new Thread()
			        {
			            public void run() 
			            {
			            	byte state=0;
			            	byte[]UID=new byte[8];
			            	int ReadType =GetReaderType();
			            	int result =HfData.reader.DisableEAS(state, UID, ReadType);
							if(result==0)
							{
								UpdateErrorToast("Disable EAS成功");
							}
							else
							{
								UpdateErrorToast("Disable EAS失败");
							}
			            };
			        }.start();
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=1;
		            	int ReadType =GetReaderType();
		            	int result =HfData.reader.DisableEAS(state, UID, ReadType);
						if(result==0)
						{
							UpdateErrorToast("Disable EAS成功");
						}
						else
						{
							UpdateErrorToast("Disable EAS失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("Disable EAS失败");
			}
		}
		else if(arg0 == btCheckEAS)
		{
			try{
				if(spuid.getSelectedItem()==null)
				{
					new Thread()
			        {
			            public void run() 
			            {
			            	byte state=0;
			            	byte[]UID=new byte[8];
			            	int ReadType =GetReaderType();
			            	int result =HfData.reader.CheckEAS(state, UID, ReadType);
							if(result==0)
							{
								UpdateErrorToast("检测到EAS报警!!!");
							}
							else if(result==1)
							{
								UpdateErrorToast("未检测到EAS报警!!!");
							}
							else if(result==1)
							{
								UpdateErrorToast("检测EAS失败");
							}
			            };
			        }.start();
					return;
				}
				String str = spuid.getSelectedItem().toString();
				str = str.substring(0,16);
				final byte[]UID =  hexStringToBytes(str);
				new Thread()
		        {
		            public void run() 
		            {
		            	byte state=1;
		            	int ReadType =GetReaderType();
		            	int result =HfData.reader.CheckEAS(state, UID, ReadType);
						if(result==0)
						{
							UpdateErrorToast("检测到EAS报警!!!");
						}
						else if(result==1)
						{
							UpdateErrorToast("未检测到EAS报警!!!");
						}
						else if(result==1)
						{
							UpdateErrorToast("检测EAS失败");
						}
		            };
		        }.start();
		        
			}catch(Exception ex)
			{
				UpdateErrorToast("检测EAS失败");
			}
		}
	}
	
	private int GetReaderType()
	{
		switch(HfData.reader.ReaderType)
		{
			case 0x76:
			{
				return 0;
			}
			case 0x16:
			case 0x11:
			case 0x12:
			case 0x13:
			{
				return 1;
			}
			default:
				return 0;
		}
	}
	//05 现在是默认就读卡了，接上高电平反而不读
	public void writelog(String log,TextView tvResult)
	{
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");// HH:mm:ss
		Date date = new Date(System.currentTimeMillis());
		String textlog = simpleDateFormat.format(date)+" "+log;
		tvResult.setText(textlog);
	}
	private boolean checkContent(int check){
		switch (check) {
		case 0:
			if(Util.isEtEmpty(tvstartblock)) return Util.showWarning(this,R.string.content_empty_warning);
			break;
		case 1:
			if(Util.isEtEmpty(tvstartblock)) return Util.showWarning(this,R.string.content_empty_warning);
			if(Util.isEtEmpty(tvblocknum)) return Util.showWarning(this,R.string.content_empty_warning);
			break;	
		case 2:
			if(Util.isEtEmpty(tvstartblock)) return Util.showWarning(this,R.string.content_empty_warning);
			if(Util.isEtEmpty(tvblocknum)) return Util.showWarning(this,R.string.content_empty_warning);
			if(Util.isEtEmpty(tvwdata)) return Util.showWarning(this,R.string.content_empty_warning);
			if(!(Util.isLenLegal(tvwdata)))
				return Util.showWarning(this,R.string.str_lenght_odd_warning);
			break;
		case 3:
			if(Util.isEtEmpty(txtAfi)) return Util.showWarning(this, R.string.content_empty_warning);
			break;
		case 4:
			if(Util.isEtEmpty(txtDsfid)) return Util.showWarning(this,R.string.content_empty_warning);
			break;
		default:
			break;
		}
		return true;
	}
	
	public String bytesToHexString(byte[] src, int offset, int length) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = offset; i < length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() == 1) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toUpperCase();
    }

	public byte[] hexStringToBytes(String hexString) {  
        if (hexString == null || hexString.equals("")) {  
            return null;  
        }  
        hexString = hexString.toUpperCase();  
        int length = hexString.length() / 2;  
        char[] hexChars = hexString.toCharArray();  
        byte[] d = new byte[length];  
        for (int i = 0; i < length; i++) {  
            int pos = i * 2;  
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
        }  
        return d;  
    }   
    private byte charToByte(char c) {  
        return (byte) "0123456789ABCDEF".indexOf(c);  
    }
    
}
